package cn.tedu.charging.device.canal;

import cn.tedu.charging.device.dao.repository.StationRepository;
import cn.tedu.charging.device.pojo.vo.ChargingStationCanalPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Slf4j
@Component
@CanalTable("charging_station") //我要同步的表
public class StationSyncHandler implements EntryHandler<ChargingStationCanalPO> {
    //1 数据库的数据有变化的时候
    //2 我得知道 insert update delete
    //3 insert redis insert
    //3 update redis update
    //3 delete redis delete

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StationRepository stationRepository;

    @Override
    public void delete(ChargingStationCanalPO stationCanalPO) {
        System.out.println("canal-delete:{}");
        log.debug("redisTemplate 删除redis");
        log.debug("canal-delete:{}",stationCanalPO);
        EntryHandler.super.delete(stationCanalPO);
    }

    @Override
    public void insert(ChargingStationCanalPO stationCanalPO) {
        System.out.println("canal-delete:{}");
        HashOperations hashOperations = redisTemplate.opsForHash();
        log.debug("hashOperations  操作 插入redis");
        log.debug("canal-insert:{}",stationCanalPO);
        EntryHandler.super.insert(stationCanalPO);
    }

    @Override
    public void update(ChargingStationCanalPO beforeStationCanalPO, ChargingStationCanalPO afterStationCanalPO ) {
        System.out.println("canal-update:{}");
        log.debug("1 stationRepository 查询最新数据，2 redisTemplate 更新redis GEO 和 详情  after:{},before:{}");
        log.debug("canal-update after:{},before:{}",afterStationCanalPO,beforeStationCanalPO);
        EntryHandler.super.update(beforeStationCanalPO, afterStationCanalPO);
    }
}
